// pages/paySocial/paySocial.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    agreeFlag: true,
    cityIndex: 0, //默认选择城市的下标
    scaleIndex: 0, //公积金缴存比例
    month: [1],
    monthIndex: 0,
    allIndex: 0, //社保类型下标
    list: [], //城市列表
    DetailsFlag: false, //
    socialDetail: {}, //社保明细对象
    firstIndex: 2, //本城市参保情况  true 首次 1 false 缴过 2
    socialFlag: false, //社保明细弹框
    beginArray: [1, 2, 3, 4, 5],
    endArray: [1, 2, 3, 4, 5],
    startIndex: 0,
    endIndex: 0,
    pictureUrl: '/image/',
    showFlag: true, //社保需要和公积金一同缴纳
    gongjijinVal: "", //公积金基数
    scrollToView: "",
    showWarn: false,
    background:""
  },
  scroll(e){
    let scrollHeight = e.detail.scrollTop
    if(scrollHeight > 30){
      this.setData({
        background:'#FFA418'
      })
    }else{
      this.setData({
        background:'transparent'
      })
    }
  },
  //我想多交点
  morePay() {
    if (this.data.agreeFlag) {
      let msg = {
        city_code: 'bu', //城市id
      }
      let query = JSON.stringify(msg)
      wx.showLoading({
        title: '等待中',
        mask: true
      })
      wx.navigateTo({
        url: `../addUser/addUser?query=${query}&fromto=social`,
      }, wx.hideLoading())
    } else {
      wx.showModal({
        content: '您需要同意相关协议',
        showCancel: false
      })
    }
  },

  // 输入公积金
  moneyBtn(e) {
    this.setData({
      gongjijinVal: e.detail.value
    })
  },
  // 缴纳最低金额
  minimumBtn() {
    this.setData({
      gongjijinVal: this.data.list[this.data.cityIndex].gongjijin_min_base
    })
  },
  // 点击查看社保明细
  socialDetails() {
    this.setData({
      DetailsFlag: true
    })
  },
  // 社保类型
  bindAllChange(e) {
    let idx = this.data.cityIndex
    let sortIdx = e.detail.value
    let list = this.data.list
    let socialDetail = {
      total: list[idx].shebao_types[sortIdx].amount,
      detail: list[idx].shebao_types[sortIdx].detail,
    }
    this.setData({
      allIndex: sortIdx,
      socialDetail
    })
  },
  // 选择缴纳多久月份
  bindMonthChange(e) {
    this.setData({
      monthIndex: Number(e.detail.value)
    })
    wx.nextTick(() => {
      this.time()
    })

  },
  // 选择缴纳城市
  bindCityChange(e) {
    let list = this.data.list
    let idx = Number(e.detail.value)
    let socialDetail = {
      total: list[idx].shebao_types[0].amount,
      detail: list[idx].shebao_types[0].detail,
    }
    this.setData({
      cityIndex: idx,
      allIndex: 0,
      scaleIndex: 0,
      monthIndex: 0,
      socialDetail
    })
    this.obtainSocialData(list[idx].shebao_types[0].id)
  },
  // 选择公积金比例
  bindScaleChange(e) {
    this.setData({
      scaleIndex: e.detail.value
    })
  },
  // 同意相关条约
  agreeBtn() {
    this.setData({
      agreeFlag: !this.data.agreeFlag
    })
  },
  // 查看相关条约
  readRules() {
    wx.showLoading({
      title: '等待中',
      mask: true
    })
    wx.navigateTo({
      url: '../rules/rules',
    }, wx.hideLoading())
  },
  // 关闭温馨提示 社保和公积金同时缴纳
  confirmBtn() {
    this.setData({
      showFlag: false
    })
  },
  // 参保情况  首次 
  firstBtn() {
    this.setData({
      firstIndex: 1
    })
  },
  // 参保情况 以前缴过
  secondBtn() {
    this.setData({
      firstIndex: 2
    })
  },
  tipConfirm() {
    this.comfirmOrder(this.data.query)
  },
  // 点击确认
  // 点击下一步
  next() {

    // gtid: this.data.query.gtid, //公积金类型
    // stid: this.data.sortArray[this.data.sortIndex].id, //社保type下的id
    // ipid: this.data.query.ipid, //参保人id
    // start_date: this.data.beginValue, //开始月份
    // count: this.data.monthCount, //缴纳长度

    // is_new: this.data.agreeIndex, //是否新参保
    let list = this.data.list
    let msg = {
      city_code: list[this.data.cityIndex].city_code, //城市id
      cityname: list[this.data.cityIndex].name, //城市名字
      stid: list[this.data.cityIndex].shebao_types[this.data.allIndex].id, //社保类型
      gtid: list[this.data.cityIndex].gongjijin_types[this.data.scaleIndex].id, //公积金类型
      count: this.data.month[this.data.monthIndex],
      start_date: this.data.same_month,
      is_new: this.data.firstIndex, //是否新参保
      gongjijin: 1
    }

    if (this.data.gongjijinVal == '') {
      this.setData({
        showWarn: true,
        scrollToView: 'gongjijin'
      })
      setTimeout(() => {
        this.setData({
          showWarn: false
        })
      }, 1000)
      return
    } else {
      let min_base = list[this.data.cityIndex].gongjijin_min_base * 100
      let max_base = list[this.data.cityIndex].gongjijin_max_base * 100
      let compareVal = this.data.gongjijinVal * 100
      // debugger
      if (compareVal >= min_base && compareVal <= max_base) {
        msg.base = compareVal / 100
      } else {
        wx.showToast({
          title: '缴纳的金额不在范围内', //提示文字
          duration: 2000, //显示时长
          mask: true, //是否显示透明蒙层，防止触摸穿透，默认：false  
          icon: 'none', //图标，支持"success"、"loading"  
        })
        return
      }
    }


    if (this.data.agreeFlag) {
      let query = JSON.stringify(msg)
      this.setData({
        query
      })
      let agreeShow = wx.getStorageSync('agreeShow')
      if (agreeShow) {
        console.log("同意了")
      } else {
        console.log("需要提醒用户")
        this.setData({
          tipsFlag: true
        })
        return
      }

      
      wx.nextTick(() =>{
        this.comfirmOrder(query)
      })
    } else {
      wx.showModal({
        content: '您需要同意相关协议',
        showCancel: false
      })
    }
  },

  comfirmOrder(query){
    wx.showLoading({
      title: '等待中',
      mask: true
    })
    wx.navigateTo({
      url: `../addUser/addUser?query=${query}&fromto=social`,
    }, wx.hideLoading())
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      mask: true,
      title: '等待中',
    })
    app.request("daijiao/shebao", {}, true).then(res => {
      wx.hideLoading()
      if (res.error_code == 0) {
        let arr = res.data;
        let socialDetail = {
          total: arr[0].shebao_types[0].amount,
          detail: arr[0].shebao_types[0].detail
        }
        app.request('daijiao/gongjijin', {}, true).then(res => {
          wx.hideLoading()
          if (res.error_code == 0) {
            let gongjijin = res.data
            for (let i in arr) {
              for (let j in gongjijin) {
                if (arr[i].city_code == gongjijin[j].city_code) {
                  arr[i].gongjijin_types = gongjijin[j].gongjijin_types
                  arr[i].gongjijin_min_base = gongjijin[j].min_base
                  arr[i].gongjijin_max_base = gongjijin[j].max_base
                }
              }
            }
            this.obtainSocialData(arr[0].shebao_types[0].id)
            this.setData({
              list: arr,
              socialDetail,
              userInfo: app.globalData.userInfo
            })
          }else{
            wx.showModal({
              content: res.error_message,
              showCancel:false
            })
          }
        })
      }else{
        wx.showModal({
          content: res.error_message,
          showCancel:false
        })
      }
    })
  },

  // 获取社保 缴纳截止日期 补缴开始日期 当前日期
  obtainSocialData(id) {
    app.request("daijiao/shebao_data", {
      stid: id,
    }, true).then(res => {
      if (res.error_code == 0) {
        console.info(res.data)
        this.setData({
          beginValue: res.data.same_month, //开始时间
          start_date: res.data.start_date, //开始时间
          same_month: res.data.same_month, //当前月份
          gongjijinVal:this.data.list[this.data.cityIndex].gongjijin_min_base,
        })
        wx.nextTick(() => {
          this.time()
        })
      }else{
        wx.showModal({
          content: res.error_message,
          showCancel:false
        })
      }
    })
  },

  time() {
    if (this.data.beginValue !== "" && this.data.monthIndex >= 0) {
      let timeDate = this.data.beginValue;
      let date = new Date(timeDate)
      let year = date.getFullYear()
      let month = date.getMonth() + 1;
      let arr = []
      arr.push(year + "-" + this.formatNumber(month))
      for (let i = 1; i < this.data.month[this.data.monthIndex]; i++) {
        month += 1
        if (month > 12) {
          arr.push(parseInt(year) + 1 + "-" + this.formatNumber(parseInt(month - 12)))
        } else {
          arr.push(year + "-" + this.formatNumber(month))
        }
      }
      console.log(arr)
      this.setData({
        lastMonth: arr[this.data.monthIndex]
      })
    }
  },
  // 月份处理
  formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  }
})